package org.way.file.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.way.base.util.CommonFileUtil;
import org.way.base.util.DateUtil;
import org.way.file.entity.FileEntity;
import org.way.file.service.IFileService;
import org.way.file.util.FileComponent;
import org.way.file.util.PoiUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class FileService implements IFileService {

    private final FileComponent fileComponent;

    private final PoiUtil poiUtil;

    @Override
    public String uploadFile(MultipartFile file) {
        FileEntity entity = fileComponent.uploadFile(file);
        new Thread(()->{
            entity.setId(IdUtil.simpleUUID());
            entity.setFileName(file.getOriginalFilename());
        }).start();
        return entity.getUrl();
    }

    @Override
    public void exportDynamicWord() {
        Map<String, Object> data = appendTableData();
        String fileName = System.currentTimeMillis() + ".docx";
        String path = poiUtil.copyTempFile("word/template.docx");
        poiUtil.exportWord(path,fileName,data,"dayRecordList","jfList","ffList");
    }

    @Override
    public void exportZip() {
        Map<String, Object> data = appendTableData();
        List<File> fileList = new ArrayList<>();
        String tempTemplateFilePath = poiUtil.copyTempFile("word/template.docx");
        String downLoadTempPath = "D:\\" + DateUtil.getToday() + "\\";
        FileUtil.mkdir(downLoadTempPath);

        XWPFTemplate template = null;
        FileOutputStream fos = null;

        try {
            for(int i=0;i<3;i++) {
                String fileName = System.currentTimeMillis() + ".docx";
                String tmpPath = downLoadTempPath + fileName;

                LoopRowTableRenderPolicy hackLoopTableRenderPolicy = new LoopRowTableRenderPolicy();
                ConfigureBuilder builder = Configure.builder();
                Configure config = builder.bind(hackLoopTableRenderPolicy,"dayRecordList","jfList","ffList").build();
                template = XWPFTemplate.compile(tempTemplateFilePath, config).render(data);
                fos = new FileOutputStream(tmpPath);
                template.write(fos);
                fileList.add(new File(tmpPath));
            }

            CommonFileUtil.downloadZip("考核表.zip",fileList);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
            throw new RuntimeException("导出zip文件失败");
        } finally {
            try {
                if(template != null) {
                    template.close();
                }
                if(fos != null) {
                    fos.flush();
                    fos.close();
                }
            } catch (IOException ignored) {

            }
        }
    }

    private Map<String, Object> appendTableData() {
        PictureRenderData pictureRenderData = Pictures.ofUrl("http://localhost:8899/way/resource/jpg/20240617/20240617154202833244256.jpg")
                .size(100, 100).create();
        Map<String, Object> map = new HashMap<>();
        map.put("title","标题");
        map.put("author","邹xx");
        map.put("recordDate","2024-06-17");
        map.put("level","一级");
        map.put("monthDay",31);
        map.put("monthScore",310);
        map.put("jfSum",10);
        map.put("ffSum",20);
        map.put("picture", pictureRenderData);

        List<Map<String, Object>> recordList = new ArrayList<>();
        List<Map<String, Object>> jfList = new ArrayList<>();
        List<Map<String, Object>> ffList = new ArrayList<>();

        LocalDate date = LocalDate.of(2024, 5, 1);
        for(int i=0;i<31;i++) {
            Map<String, Object> record = new HashMap<>();
            record.put("dayOfMonth", DateUtil.toStr(date.plusDays(i)));
            record.put("zgsj",6);
            record.put("jdkf",1);
            record.put("jyjz",1);
            record.put("kfld",2);
            record.put("tbjj",0);
            record.put("dayOfScore",10);
            recordList.add(record);
        }

        Map<String, Object> jf = new HashMap<>();
        jf.put("recordDate","05-22");
        jf.put("project","坦白检举");
        jf.put("reason","老实交代犯罪经历");
        jf.put("content","第三十八条");
        jf.put("grade","10");
        jfList.add(jf);

        Map<String, Object> ff = new HashMap<>();
        ff.put("recordDate","05-12");
        ff.put("project","康复劳动");
        ff.put("reason","生产劳动不积极不合格的");
        ff.put("content","第二十二条");
        ff.put("grade","20");
        ffList.add(ff);

        map.put("dayRecordList",recordList);
        map.put("jfList",jfList);
        map.put("ffList",ffList);
        return map;
    }
}
